#import "SendSuggestUI.h"
#import "ConfigData.h"
#import "Tools.h"
#import "JSON.h"
#import "Tools.h"
#import "UIView+Toast.h"
@implementation SendSuggestUI
@synthesize string,textView,mp;
-(void)sendSuggestFn{
    NSString* msg=self.textView.text;
    if (msg.length==0) {
        [self.view makeToast:@"请输入意见" duration:1.0 position:@"center"];
        return ;
    }
    NSString* mpStr=self.mp.text;
    NSLog(@"msg==%@,mp=%@",msg,mpStr);
    //
    
    NSLog(@"开始登录..");
    NSString* loginURL=HOST;
    loginURL=[loginURL stringByAppendingString:@"index.php/appSuggest/saveNewSuggest"];
    NSLog(@"放松=%@",loginURL);
    NSURL* url=[[NSURL alloc] initWithString:loginURL];
    ASIFormDataRequest *req=[ASIFormDataRequest requestWithURL:url];
    
    [req addPostValue:msg forKey:@"content"];
    
    [req startSynchronous];
    NSError *error = [req error];
    if (!error) {
        NSString *response = [req responseString];
        NSLog(@"登录返回结果%@",response);
        
        NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithDictionary:[response JSONValue]];
        
        NSString *codeStr=[dic objectForKey:@"code"];
        int code=[codeStr intValue];
        NSLog(@"code==%d",code);
        if(code==1){//登录成功
            NSString *uidStr=[dic objectForKey:@"uid"];
            
            
            UIAlertView * successTip = [[UIAlertView alloc] initWithTitle:@"信息提示:" message:@"发送成功！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [successTip show];
            
            [self dismissModalViewControllerAnimated:NO];
            
            
            
        }else if(code==2){//用户名或密码失败
            NSLog(@"登录失败..");
            UIAlertView * successTip = [[UIAlertView alloc] initWithTitle:@"信息提示:" message:@" 发送失败！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [successTip show];
            
        }
    }
    
    
    //
}
//关闭键盘
-(void)resignKeyboard{
    [self.textView resignFirstResponder];
    [self.mp resignFirstResponder];
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self.mp resignFirstResponder];
    return YES;
}
-(void)backFn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)prefersStatusBarHidden
{
    return YES;//隐藏为YES，显示为NO
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor whiteColor];
    
    UILabel* titleLb=[[UILabel alloc] initWithFrame:CGRectMake(100, 0, 120, 44)];
    titleLb.text=@"意见反馈";
    titleLb.backgroundColor=[UIColor clearColor];
    titleLb.textAlignment=NSTextAlignmentCenter;
    titleLb.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=titleLb;
    
    
    //////////////
    CGRect frame_img= CGRectMake(0, 1, 25/2, 41/2);
    UIImageView* backView=[[UIImageView alloc] initWithFrame:frame_img];
    backView.image=[UIImage imageNamed:@"返回"];
    
    CGRect frameBtn= CGRectMake(0, 0,50, 41/2);
    UIButton* homeBtn= [[UIButton alloc] initWithFrame:frameBtn];
    
    [homeBtn addTarget:self action:@selector(clickOnBackBut) forControlEvents:UIControlEventTouchUpInside];
    
    [homeBtn addSubview:backView];
    UILabel* backLb=[[UILabel alloc] initWithFrame:CGRectMake(14, 1, 28, 20)];
    backLb.text=@"返回";
    backLb.font=[UIFont boldSystemFontOfSize:14];
    backLb.textColor=[UIColor whiteColor];
    backLb.backgroundColor=[UIColor clearColor];
    [homeBtn addSubview:backLb];
    
    
    UIBarButtonItem* barHomeBtn= [[UIBarButtonItem alloc] initWithCustomView:homeBtn];
    
    self.navigationItem.leftBarButtonItem=barHomeBtn;
    
    //
    UIButton* btn= [[UIButton alloc] init];
    btn.frame=CGRectMake(172.0, 2, 66.0, 40.0);
    UIImage* img = [UIImage imageNamed: @"23.png"] ;
    [btn setBackgroundImage:img forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(sendSuggestFn) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"发 送" forState:UIControlStateNormal];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    
    self.navigationItem.rightBarButtonItem= rightItem;
    
    
    
    float iosVersion=[Tools getIOSVersion];
    int y=0;
    if (iosVersion>=7.0) {
        y=24;
    }
    
    UIImageView *iv=[[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    iv.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignKeyboard)];
    [iv addGestureRecognizer:singleTap];
    [self.view addSubview:iv];
    //
    UIImage* textArea=[UIImage imageNamed:@"textarea2.png"];
    UIImageView*  textAreaView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 100)];
    textAreaView.image=textArea;
    [self.view addSubview:textAreaView];
    //
    UIPlaceholderTextView *theTextView = [[UIPlaceholderTextView alloc] initWithFrame:CGRectMake (12, 12, 300-4, 100-4)];
    
    theTextView.editable = YES;
    theTextView.placeholder=@"欢迎您反馈意见及问题，这将是我们产品进步的动力";
    theTextView.font = [UIFont systemFontOfSize:14.0];
    
    [theTextView becomeFirstResponder];
    self.textView = theTextView;
    
    textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    //textView.layer.borderWidth =1.0;
    //textView.layer.cornerRadius =5.0;
    
    //
    [self.view addSubview:theTextView];
    
    
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [textView resignFirstResponder];
}

-(void)clickOnBackBut{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


